//
//  HomeGoodsListViewController.m
//  ZJControlsClass
//
//  Created by mac on 2024/12/2.
//

#import "HomeGoodsListViewController.h"
#import "HomeTransferOrderViewController.h"
#import "HomeTransactHeadViewCollectionReusableView.h"
#import "HomeTransactPersonScrViewCollectionViewCell.h"
#import "HomeTransactPersonSectionTwoCollectionViewCell.h"
#import "HomeUnitRecordViewController.h"


#import "HomeTransactPersonModel.h"
#import "HomeNetWorking.h"




@interface HomeGoodsListViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>


@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong) NSMutableArray * dataSource;
@property (nonatomic,strong) HomeMyModuleNetWorking * manager;


@end

@implementation HomeGoodsListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"办理入场工具货物";
    
    [self setUpUI];
    [self loadNet];
    
}

-(void)setUpUI{
    
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).with.offset(ALIGN_WIDTH_PT(12));
        make.right.equalTo(self.view).with.offset(-ALIGN_WIDTH_PT(12));
        make.top.bottom.equalTo(self.view);
    }];
    
}

#pragma mark -- 网络请求
-(void)loadNet{
    
    NSDictionary * dic = @{@"groupId":@"4"};
    [self.manager dispatchTaskWithParam:dic completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
        if (responseObject && [responseObject[@"code"] integerValue] == 200) {

            NSMutableArray * dataArray = [NSMutableArray arrayWithArray: [HomeTransactPersonModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]]];
            NSMutableArray * oneArr = @[].mutableCopy;
            
            [dataArray enumerateObjectsUsingBlock:^(HomeTransactPersonModel  * obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([obj.groupId intValue] == 4){
                    obj.headTitle = @"工具货物";
                    [oneArr addObject:obj];
                }
            }];
            
            [self.dataSource addObject:@[[HomeTransactPersonModel new]]]; // 滚动试图 创建一个空的数据
            [self.dataSource addObject:oneArr];
            
            [self.collectionView reloadData];
        }else{
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:responseObject[@"msg"] buttonAction:^{
                
            }];
        }
    }];
    
}

#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return self.dataSource.count;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return [self.dataSource[section] count];
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    UICollectionViewCell * sectionCell = nil;
    
    HomeTransactPersonModel * model  = self.dataSource[indexPath.section][indexPath.row];
    
    if (indexPath.section == 0){
        HomeTransactPersonScrViewCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeTransactPersonScrViewCollectionViewCell" forIndexPath:indexPath];
        
        
        sectionCell = cell;
        
        
        return sectionCell;
        
    }else{
        
        HomeTransactPersonSectionTwoCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeTransactPersonSectionTwoCollectionViewCell" forIndexPath:indexPath];
//        cell.follewView.backgroundColor = COLOR_HEX(model.backColor);
        cell.follewView.backgroundColor = COLOR_RGB(232, 247, 237);
        cell.titleLabel.text = model.name;
        cell.rightImg.image = [UIImage imageNamed:@"Vector"];
//        cell.iconImg.image = [UIImage imageNamed:model.img];
        cell.iconImg.image = [UIImage imageNamed:@"货物1"];
        sectionCell = cell;
        
        
        return sectionCell;
        
    }
        
    return sectionCell;
    
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
     
    
        HomeTransactPersonModel * model  = self.dataSource[indexPath.section][indexPath.row];
    
        if ([model.id isEqualToString:@"997"]){
            HomeTransferOrderViewController * vc = [HomeTransferOrderViewController new];
            vc.Id = model.id;
            [[SaViewControllerUtils currentViewController].navigationController pushViewController:vc animated:YES];
        }else{
            HomeUnitRecordViewController * vc = [HomeUnitRecordViewController new];
            vc.moduleId = model.id;
            vc.name = model.name;
            [[SaViewControllerUtils currentViewController].navigationController pushViewController:vc animated:YES];
        }
    
    
    
}
 /* 头部视图 */
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView * reusableView = nil;
    

    if (indexPath.section != 0){
        if (kind == UICollectionElementKindSectionHeader){
            HomeTransactPersonModel * model  = self.dataSource[indexPath.section][indexPath.row];
            HomeTransactHeadViewCollectionReusableView * headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HomeTransactHeadViewCollectionReusableView" forIndexPath:indexPath];
            headView.titleLabel.text = model.headTitle;

            reusableView = headView;
        }
    }
    
    return  reusableView;
}

/* 头部试图高度 */
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section != 0) return CGSizeMake(SCREEN_WIDTH, ALIGN_HEIGHT_PT(40));
    return CGSizeMake(0, 0);
}



#pragma mark - item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
   
    if (indexPath.section == 0){
        return CGSizeMake(SCREEN_WIDTH, ALIGN_HEIGHT_PT(200));
    }else{
        return CGSizeMake((SCREEN_WIDTH - ALIGN_WIDTH_PT(35)) / 3, ALIGN_HEIGHT_PT(110));
    }
    
}

#pragma mark - <UICollectionViewDelegateFlowLayout>
#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return  ALIGN_WIDTH_PT(5);
}
#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return  ALIGN_WIDTH_PT(10);
}


#pragma mark -- 懒加载

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        
        //cell
        [_collectionView registerClass:[HomeTransactPersonScrViewCollectionViewCell class] forCellWithReuseIdentifier:@"HomeTransactPersonScrViewCollectionViewCell"];
        [_collectionView registerClass:[HomeTransactPersonSectionTwoCollectionViewCell class] forCellWithReuseIdentifier:@"HomeTransactPersonSectionTwoCollectionViewCell"];
        
        // 头部视图
        [_collectionView registerClass:[HomeTransactHeadViewCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HomeTransactHeadViewCollectionReusableView"];
        
    }
    return _collectionView;
}


-(NSMutableArray *)dataSource{
    if(!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

-(HomeMyModuleNetWorking *)manager{
    if (!_manager) {
        _manager = [HomeMyModuleNetWorking new];
    }
    return _manager;
}



@end
